using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#if XBOX
#else
using System.Windows;
#endif


namespace Perovich.GameObjects.Controls
{

    /// <summary>
    /// The <see cref="TextLabel"/> class is a simple <see cref="Control"/> that displays text.
    /// </summary>
    /// <remarks>The <see cref="TextLabel"/> currently will take up as much space as the text requires plus the margin.</remarks>
    public class TextLabel : Control
    {
        SpriteFont _font;
        string _text;
        
        /// <summary>
        /// Gets or sets the text to be displayed.
        /// </summary>
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        /// <summary>
        /// Gets or sets the <see cref="SpriteFont"/> used to draw the text.
        /// </summary>
        /// <remarks>If no <see cref="SpriteFont"/> is provided a default one will be used.</remarks>
        public SpriteFont Font
        {
            get { return _font; }
            set { _font = value; }
        }


        /// <summary>
        /// Gets the total width of the text.
        /// </summary>
        public new float Width
        {
            get
            {
                return Font.MeasureString(_text).X + (float)Margin.Left + (float)Margin.Right;
            }
        }

        /// <summary>
        /// Gets the total height of the text.
        /// </summary>
        public new float Height
        {
            get
            {
                return Font.MeasureString(_text).Y + (float)Margin.Top + (float)Margin.Bottom;
            }
        }

        /// <summary>
        /// Gets or sets the <see cref="System.Windows.HorizontalAlignment"/> of the text.
        /// </summary>
        public HorizontalAlignment HorizontalAlignment
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the <see cref="VerticalAlignment"/> of the text.
        /// </summary>
        public VerticalAlignment VerticalAlignment
        {
            get;
            set;
        }

        /// <summary>
        /// Constructs a new instance of the <see cref="TextLabel"/> class.
        /// </summary>
        /// <param name="game">
        ///     <see cref="Perovich.GameObjects.Interfaces.ICanHoldComponents"/> instance that this <see cref="Control"/> will be constructed for.
        /// </param>
        public TextLabel(Interfaces.ICanHoldComponents game)
            : base(game)
        {
            HorizontalAlignment = HorizontalAlignment.Left;
            VerticalAlignment = VerticalAlignment.Top;
        }
        

        /// <summary>
        /// Allows the <see cref="TextLabel"/> to draw itself.
        /// </summary>
        /// <param name="gameTime">
        ///     Provides a snapshot of the game's timing values.
        /// </param>
        public override void Draw(GameTime gameTime)
        {
            Vector2 drawOrigin = new Vector2();
            switch (HorizontalAlignment)
            {
                case HorizontalAlignment.Left:
                    {
                        drawOrigin.X = - (float)Margin.Left;
                        break;
                    }
                case HorizontalAlignment.Center:
                    {
                        drawOrigin.X = Width / 2;
                        break;
                    }
                case HorizontalAlignment.Right:
                    {
                        drawOrigin.X = Width + (float)Margin.Right;
                        break;
                    }
                case HorizontalAlignment.Stretch:
                    {
                        drawOrigin.X = Width / 2;
                        break;
                    }
            }
            switch (VerticalAlignment)
            {
                case VerticalAlignment.Top:
                    {
                        drawOrigin.Y = - (float)Margin.Top;
                        break;
                    }
                case VerticalAlignment.Center:
                    {
                        drawOrigin.Y = Height / 2;
                        break;
                    }
                case VerticalAlignment.Bottom:
                    {
                        drawOrigin.Y = Height + (float)Margin.Bottom;
                        break;
                    }
                case VerticalAlignment.Stretch:
                    {
                        drawOrigin.Y = Height / 2;
                        break;
                    }
            }
            SBatches.CurrentBatch.DrawString(_font, _text, Position, ForegroundColor, Rotation, drawOrigin, Scale, SpriteEffects.None, Depth);
            base.Draw(gameTime);
        }

        /// <summary>
        /// Loads the content for the <see cref="TextLabel"/> class.
        /// </summary>
        protected override void LoadContent()
        {
            Font = GameObjectsContent.Load<SpriteFont>("defaultFont");

            base.LoadContent();
        }
    }
}
